package com.ybwh.springboot1.controller;

import javax.validation.Valid;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ybwh.springboot1.dao.AreaMapper;
import com.ybwh.springboot1.model.po.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ybwh.springboot1.common.Response;
import com.ybwh.springboot1.model.vo.AreaVo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@Controller
@RequestMapping("/area")
public class AreaController {
	@Autowired
	private AreaMapper dao;


	@ResponseBody
	@RequestMapping("/validate")
	public Response<Object> validate(@Validated @ModelAttribute AreaVo areaVo, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			for(ObjectError error : bindingResult.getAllErrors()) {
				return Response.failedResp(error.getDefaultMessage());
			}
		}

		return Response.successResp(null);
	}



	@ResponseBody
	@RequestMapping("/query")
	public Response<Object> query() {
		Page<Area> page = dao.selectByPageNumSize(new Area(),1, 10);
		PageInfo<Area> pageInfo = new PageInfo<>(page);

		return Response.successResp(pageInfo);
	}



}
